package com.hmall.user.interceptor;

import com.hmall.user.util.ThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 定义拦截器
 */
@Component
@Slf4j
public class MyIntercpetor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
       String header = request.getHeader("authorization");
        if (header != null) {
            ThreadLocalUtil.setUserId(Long.valueOf(header));
        }
        return true;
    }
}
